package com.minimalist.tc.mapper;

import com.minimalist.common.entity.tc.entity.po.TcOrder;
import com.minimalist.common.entity.tc.entity.vo.order.TcOrderQueryVO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

/**
 * 停车订单表 映射层。
 *
 * @author 11987
 * @since 2025-04-07
 */
public interface TcOrderMapper extends BaseMapper<TcOrder>{

    /**
     * 根据订单ID查询订单信息。
     *
     * @param orderId 订单ID
     * @return 订单信息
     */
    default TcOrder selectOrderByOrderId(Long orderId){
        return selectOneByQuery(QueryWrapper.create().eq(TcOrder::getOrderId, orderId));
    }

    /**
     * 根据订单ID删除订单信息。
     *
     * @param orderId 订单ID
     */
    default void deleteOrderByOrderId(Long orderId){
        deleteByQuery(QueryWrapper.create().eq(TcOrder::getOrderId, orderId));
    }

    /**
     * 根据订单ID更新订单信息。
     *
     * @param tcOrder 订单信息
     */
    default void updateOrderByOrderId(TcOrder tcOrder){
        updateByQuery(tcOrder, QueryWrapper.create().eq(TcOrder::getOrderId, tcOrder.getOrderId()));
    }

    /**
     * 分页查询订单列表。
     *
     * @param queryVO 查询实体
     * @return 分页结果
     */
    default Page<TcOrder> selectPageOrderList(TcOrderQueryVO queryVO){
        QueryWrapper wrapper = QueryWrapper.create()
                .like(TcOrder::getOrderNumber, queryVO.getOrderNumber())
                .like(TcOrder::getBerthNumber, queryVO.getBerthNumber())
                .like(TcOrder::getPointCharge, queryVO.getPointCharge())
                .like(TcOrder::getPlateNumber, queryVO.getPlateNumber());

        // 处理驶入时间范围
        if (queryVO.getEnterDateRange() != null && queryVO.getEnterDateRange().length == 2){
            if (queryVO.getEnterDateRange()[0] != null){
                wrapper.ge(TcOrder::getEnterDate, queryVO.getEnterDateRange()[0]);
            }
            if (queryVO.getEnterDateRange()[1] != null){
                wrapper.le(TcOrder::getEnterDate, queryVO.getEnterDateRange()[1]);
            }
        }

        // 处理驶离时间范围
        if (queryVO.getExitDateRange() != null && queryVO.getExitDateRange().length == 2){
            if (queryVO.getExitDateRange()[0] != null){
                wrapper.ge(TcOrder::getExitDate, queryVO.getExitDateRange()[0]);
            }
            if (queryVO.getExitDateRange()[1] != null){
                wrapper.le(TcOrder::getExitDate, queryVO.getExitDateRange()[1]);
            }
        }

        return paginate(queryVO.getPageNum(), queryVO.getPageSize(), wrapper);
    }
}
